package com.edu.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.edu.dao.ChartingRecordsDao;
import com.edu.dao.UserDao;
import com.edu.dao.impl.ChartingRecordsDaoImpl;
import com.edu.dao.impl.UserDaoImpl;
import com.edu.entity.ChartingRecords;
import com.edu.entity.User;
import com.edu.service.ChartingRecordsService;

public class ChartingRecordsServiceImpl implements ChartingRecordsService {
	private ChartingRecordsDao crd = new ChartingRecordsDaoImpl();
	@Override
	public boolean addChartingRecords(ChartingRecords cr) {
		// TODO Auto-generated method stub
		int rowNum = crd.insertInto(cr);
		if(rowNum==1) {
			return true;
		}
		return false;
	}
	@Override
	public List<ChartingRecords> getChartingRecords(ChartingRecords cr) {
		// TODO Auto-generated method stub
		return crd.selectChartingRecords(cr);
	}
	@Override
	public Set<User> selectMyChartingObjs(String sendUsername) {
		UserDao ud = new UserDaoImpl();
		Set<String> usernames = new HashSet<String>();
		Set<User> usersSet = new HashSet<User>();
		List<ChartingRecords> reciveusername = crd.selectMyChartingObjs(sendUsername);
//		System.out.println("8********");
//		System.out.println(reciveusername);
//		System.out.println("___________");
		for (int i = 0; i < reciveusername.size(); i++) {
			User u = new User();
			if(reciveusername.get(i).getSendUsername()!=sendUsername) {
				u.setUsername(reciveusername.get(i).getSendUsername());
			}else {
				u.setUsername(reciveusername.get(i).getReciveusername());
			}
			User user = ud.sqlLogin(u);
			usernames.add(user.getUsername());
			
		}
		for (int i = 0; i < reciveusername.size(); i++) {
			User u = new User();
			if(reciveusername.get(i).getSendUsername()==sendUsername) {
				u.setUsername(reciveusername.get(i).getSendUsername());
			}else {
				u.setUsername(reciveusername.get(i).getReciveusername());
			}
			User user = ud.sqlLogin(u);
			usernames.add(user.getUsername());
			
		}
		for (String str : usernames) {
			User u = new User();
			u.setUsername(str);
			User user = ud.sqlLogin(u);
			usersSet.add(user);
		}
		System.out.println(usersSet.toString());
		return usersSet;
	}

}
